//
//  SendWeiboController.swift
//  WEIBO_SWIFT
//
//  Created by 岳挺 on 15/7/8.
//  Copyright © 2015年 yueting. All rights reserved.
//

import UIKit
import FFAutoLayout

class SendWeiboController: UIViewController,UITextViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareNavigationBar()
        prepareTextView()
    }
    
    func prepareNavigationBar() {
        //左侧按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelSend")
        //右侧按钮
        let btn = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendWeibo")
        btn.enabled = textViewIsEmpty()
        navigationItem.rightBarButtonItem = btn
        //中间文字view
        let v = UIView(frame: CGRectMake(0, 0, 100, 40))
        let label1 = UILabel(title: "发微博", fontSize: 14.0, titleColor: UIColor.darkGrayColor())
        let label2 = UILabel(title:sharedUserAccount?.name ?? nil , fontSize: 12.0, titleColor: UIColor.lightGrayColor())
        
        v.addSubview(label1)
        v.addSubview(label2)
        
        label1.ff_AlignInner(ff_AlignType.TopCenter, referView: v, size: nil)
        label2.ff_AlignInner(ff_AlignType.BottomCenter, referView: v, size: nil)
        navigationItem.titleView = v
    }
    
    func prepareTextView() {
        
        textView.alwaysBounceVertical = true
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        textView.inputAccessoryView = toolBar
        textView.delegate = self
        view.addSubview(textView)
        placeHolder.highlighted = textViewIsEmpty()
        textView.addSubview(placeHolder)
        
        textView.ff_Fill(view)
        placeHolder.ff_AlignInner(ff_AlignType.TopLeft, referView: textView, size: nil, offset: CGPoint(x: 8, y: 8))
        
    }
    
    func textViewDidChange(textView: UITextView) {
        navigationItem.rightBarButtonItem?.enabled = !textView.text.isEmpty
        placeHolder.hidden = !textView.text.isEmpty
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        textView.resignFirstResponder()
    }
    
//    func prepareToolBar() {
//        var items = [UIBarButtonItem]()
//        let arr = [["imageName" : "compose_toolbar_picture", "actionName" : "selectPicture"],
//            ["imageName" : "compose_mentionbutton_background"],
//            ["imageName" : "compose_trendbutton_background"],
//            ["imageName" :"compose_emoticonbutton_background","actionName" : "selectEmoticon"],
//            ["imageName" : "compose_addbutton_background"]]
//        for dict in arr {
//            let item = UIBarButtonItem(imageName: dict["imageName"]!, highlightImageName: nil, target: self, actionName: dict["actionName"])
//            items.append(item)
//            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
//        }
//        items.removeLast()
//        toolBar.items = items
//        
//    }
    
    func sendWeibo() {
        print(__FUNCTION__)
    }
    
    func cancelSend() {
        print(__FUNCTION__)
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func selectPicture() {
        print(__FUNCTION__)
    }
    
    func selectEmoticon() {
        print(__FUNCTION__)
    }
    
    func textViewIsEmpty() ->Bool {
        if textView.text.isEmpty {
            print(textView.text)
            return false
        }else{
            return true
        }
    }

    override func loadView() {
        super.loadView()
        view.backgroundColor = UIColor.yellowColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    private lazy var textView = UITextView()
    private lazy var placeHolder : UILabel  = UILabel(title: "分享新鲜事...", fontSize: 15.0, titleColor: UIColor.darkGrayColor())
    private lazy var toolBar : UIToolbar = {
        let tool = UIToolbar(frame: CGRectMake(0, 0, 0, 44))
        tool.barTintColor = UIColor(red: 235.0/256.0 , green: 235.0/256.0, blue: 235.0/256.0, alpha: 1.0)
        
        var items = [UIBarButtonItem]()
        let arr = [["imageName" : "compose_toolbar_picture", "actionName" : "selectPicture"],
            ["imageName" : "compose_mentionbutton_background"],
            ["imageName" : "compose_trendbutton_background"],
            ["imageName" :"compose_emoticonbutton_background","actionName" : "selectEmoticon"],
            ["imageName" : "compose_addbutton_background"]]
        for dict in arr {
            let item = UIBarButtonItem(imageName: dict["imageName"]!, highlightImageName: nil, target: self, actionName: dict["actionName"])
            items.append(item)
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        items.removeLast()
        tool.items = items
        return tool
    }()
    
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}
